package com.hnjd.headline.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnjd.headline.mapper.HeadlineMapper;
import com.hnjd.headline.pojo.Headline;
import com.hnjd.headline.pojo.dto.HeadlineDetail;
import com.hnjd.headline.pojo.dto.HeadlineDto;
import com.hnjd.headline.pojo.dto.HeadlineEditEcho;
import com.hnjd.headline.pojo.dto.PortalVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.hnjd.headline.service.HeadlineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 服务接口实现
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2025-06-16 15:53:03
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline> implements HeadlineService {

    @Autowired
    private final HeadlineMapper headlineMapper;

    @Override
    public IPage<HeadlineDto> findNewsPage(PortalVo portalVo) {

        // 分页
        IPage<HeadlineDto> page = new Page<>(portalVo.getPageNum(), portalVo.getPageSize());

        // 执行查询
        headlineMapper.selectHeadlineToPage(page, portalVo);

        return page;
    }

    @Override
    public HeadlineDetail getHeadlineById(Integer hid) {

        // 查询
        HeadlineDetail headlineDetail = headlineMapper.selectHeadlineDetailById(hid);

        if (headlineDetail == null) {
            return null;
        }

        // 准备更新数据
        Headline updateHeadline = new Headline();
        updateHeadline.setHid(headlineDetail.getHid());
        // 阅读量+1
        updateHeadline.setPageViews(headlineDetail.getPageViews() + 1);
        updateHeadline.setVersion(headlineDetail.getVersion());

        updateById(updateHeadline);

        return headlineDetail;
    }

    @Override
    public HeadlineEditEcho findHeadlineEditEchoByHid(Integer hid) {
        return headlineMapper.selectHeadlineEditById(hid);
    }

}